package pokemon;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

@SuppressWarnings("serial")
public class Pokedex extends ArrayList<Pokemon>
{	
	public static AbilityList abil = new AbilityList();
	public static TypeChart typeChart = new TypeChart();
	public static HashMap<String, Boolean> tiers = new HashMap<String, Boolean>();
	
	private final String TIERS_LOCATION = "Tiers";	
	private HashMap<String, ArrayList<String>> tierList = new HashMap<String, ArrayList<String>>();
	
	public Pokedex()
	{
		super();
		readTierLists();
	}
	
	public static void populateList()
	{
		abil.calculateAbilities();
	}
	
	public Pokemon getPokemon(String name)
	{
		Collections.sort(this);
		int index = Collections.binarySearch(this, new Pokemon(name));
		if(index >= 0)
		{
			return this.get(index);
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * Reads the tier listings from text into a HashMap
	 */
	public void readTierLists()
	{
		File tiersDirectory = new File(TIERS_LOCATION);
		Scanner reader;
		for(File tier : tiersDirectory.listFiles())
		{
			try
			{
				reader = new Scanner(tier);
				
				// Initializes the tier bitmask with a value of true for each tier
				tiers.put(tier.getName(), true);
				
				tierList.put(tier.getName(), new ArrayList<String>());
				while(reader.hasNextLine())
				{
					tierList.get(tier.getName()).add(reader.nextLine());
				}
			}
			catch(FileNotFoundException e)
			{
				System.err.println("File not found.");
				System.exit(1);
			}
		}
	}
	
	/**
	 * Sets which Pokemon show up in the pokedex based on their tier
	 * 
	 * @param wantedTiers - A list of the tiers wanted
	 */
	public Pokedex newPokedexWithTiers(ArrayList<String> wantedTiers)
	{
		Pokedex dex = new Pokedex();
		for(String tier : tierList.keySet())
		{
			if(wantedTiers.contains(tier))
			{
				for(String poke : tierList.get(tier))
				{
					dex.add(this.getPokemon(poke));
					if(this.getPokemon(poke)==null)
						System.out.println(poke);
				}
			}
		}
		return dex;
	}
}
